package gate

import (
	"base"
	. "center_server/logic/internal"
	"leaf/gate"
	"proto/server/center"
	"proto/server/gate"
)

type gateService struct {
}

func init() {
	service := new(gateService)
	Protocol.Register(new(proto_server_center.Login), service.login)
	Protocol.Register(new(proto_server_center.Logout), service.logout)
}

func (*gateService) login(args []interface{}) {
	msg := args[0].(*proto_server_center.Login)
	base.Logger.Release("gate service login, %s", msg)

	agent := args[1].(gate.Agent)
	server := agent.UserData().(*gateServer)
	if err := system.login(server, msg); err != nil {
		result := base.ParseError(err)
		code := result.Code

		response := new(proto_server_gate.OnLogin)
		response.ClientId = msg.ClientId
		response.Code = code
		server.WriteMsg(response)
	}
}

func (*gateService) logout(args []interface{}) {
	msg := args[0].(*proto_server_center.Logout)
	base.Logger.Release("gate service logout, %s", msg)

	agent := args[1].(gate.Agent)
	server := agent.UserData().(*gateServer)
	clientId := base.ClientId(msg.ClientId)

	system.logout(server, clientId)
}
